package mobi.hami.huapin.controller;

import com.alibaba.druid.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.model.Productsku;
import mobi.hami.huapin.service.ProductSkuService;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping(value = "/shot", produces = "application/json")
@Api(description = "商品skuApi")
public class ProductSkuController {
    @Autowired
    private ProductSkuService productSkuService;

    @RequestMapping(value = "/productSkuList.do", method = RequestMethod.GET)
    @ApiOperation(value = "查询商品下的Sku", notes = " ")
    public Result getProductSkuList( Integer productid,String  name,Integer isdelete) {
        Productsku sku= new Productsku();
        sku.setProductid(productid);
        if(StringUtils.isNumber(name))
        sku.setId(Integer.parseInt(name));
        else {
            sku.setSkuname(name);
        }
        sku.setDeleted(isdelete);
        return Results.successWithData(productSkuService.getProductSkuList(sku), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/getProductSkuAdd.do", method = RequestMethod.POST)
    @ApiOperation(value = "添加SKu", notes = " ")
    public Result getProductSkuAdd(@RequestBody Productsku productsku) {
        Date date=new Date();
        productsku.setSkucode((productsku.getSkuname()+productsku.getProductid()).hashCode());
        System.out.print(  productsku.getProductcode());
        productsku.setGmt_create(date);
        productsku.setGmt_modified(date);
        return Results.successWithData(productSkuService.getProductSkuAdd(productsku), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/deltesku.do", method = RequestMethod.POST)
    @ApiOperation(value = "删除sku", notes = " ")
    public  Result deltesku(@RequestBody Productsku productsku) {
        Date date=new Date();
        productsku.setGmt_modified(date);
        return Results.successWithData(productSkuService.deleteSku(productsku), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/sku/update.do", method = RequestMethod.POST)
    @ApiOperation(value = "sku信息修改", notes = " ")
    public  Result skuUpdate(@RequestBody Productsku productsku) {
        Date date=new Date();
        productsku.setGmt_modified(date);
        return Results.successWithData(productSkuService.skuUpdate(productsku), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
}
